package com.infocollect.project.api.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.infocollect.common.utils.ServletUtils;
import com.infocollect.framework.security.LoginUser;
import com.infocollect.framework.security.service.TokenService;
import com.infocollect.project.api.entity.TappUserControlEntity;
import com.infocollect.project.api.helper.CurrentHelp;
import com.infocollect.project.api.mapper.TappUserControlMapper;
import com.infocollect.project.api.req.UserControlPage;
import com.infocollect.project.api.service.ITappUserControlService;
import com.infocollect.project.api.vo.CurrentUserInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 刘亚平
 * @since 2021-06-23
 */
@Service
public class TappUserControlServiceImpl extends ServiceImpl<TappUserControlMapper, TappUserControlEntity> implements ITappUserControlService {
    @Autowired
    private TokenService tokenService;
    /**
    * 通过主键获取数据
    *
    * @param id 主键
    * @return 是否成功
    */
    @Override
    public  TappUserControlEntity getById(Long id){
          return    baseMapper.selectById(id);
    }
    /**
     * 通过主键删除数据
     *
     * @param fid 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long fid) {
            return this.baseMapper.deleteById(fid) > 0;
    }

    /***
     * 用户组件入库
     * @param req
     * @return
     */
    @Override
    public  boolean addUserControlInfo(TappUserControlEntity req){
//        CurrentUserInfoVO currentUserInfo = CurrentHelp.Ins.GetUser();
        LoginUser currentUserInfo = tokenService.getLoginUser(ServletUtils.getRequest());
       // req.setFid(IdWorker.getId());
        req.setFcreateby(currentUserInfo.getUserName());
        req.setFlastupdateby(currentUserInfo.getUserName());
        req.setFcreationdate(new Date());
        req.setFlastupdatedate(new Date());
        this.baseMapper.insert(req);
        return true;
    }

    /**
     * 查询用户控件列表
     * @param page
     * @return
     */
    @Override
    public UserControlPage queryUserControlList(UserControlPage page){
        LambdaQueryWrapper<TappUserControlEntity> lambdaQueryWrapper = new LambdaQueryWrapper();
        if(StringUtils.isNotBlank(page.getUserControlName())) {
            lambdaQueryWrapper.like(TappUserControlEntity::getControlname, page.getUserControlName());
        }
        Page<TappUserControlEntity>     userControlPage = baseMapper.selectPage(page,lambdaQueryWrapper);
        page.setRecords(userControlPage.getRecords());
        ///page.setTotal(userControlPage.getTotal());
        return page;

    }
}

